# ============================================================================ #
# Date     : 19 May 2024                                                       #
# Authors  : Royce Carroll, Yen-Chieh Liao and Li Tang                         #
# Purpose  : cleaned_lr_BES_CHES.csv                                           #
# Outputs  : figure1_waves.png                                                 #
# ============================================================================ #


# Load required libraries ------------------------------------------------------

library(readr)
library(ggplot2)
library(scales)
library(ggpub)

# Read the CSV file ------------------------------------------------------------

lr_waves <- read_csv("cleaned_lr_BES_CHES.csv")

p1 <- ggplot(lr_waves, aes(x = factor(wave), y = value, colour = type)) +
  coord_flip()+
  geom_text(aes(label=party), show_guide = T, fontface='bold') +
  scale_x_discrete(limits = c(4, 6, 7, 8, 9, 10, 15, 16, 17) ) +
  theme(axis.title.y = element_blank()) +
  facet_grid(vars(wave), scales = "free",
             labeller =label_both) +
  ylim(c(0, 10)) +
  theme(plot.title = element_text(hjust = -1))  +
  theme(
    panel.border = element_blank(), 
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(), 
    axis.line = element_line(colour = "black")
  ) +
  scale_y_continuous(breaks= pretty_breaks()) +
  theme(
    plot.title = element_text(size = 14, face = "bold"),
    text = element_text(size = 14),
    axis.text.x = element_text(size = 12),
    axis.text.y = element_blank(), 
    axis.text = element_text(size = 12),
    axis.title = element_text(size = 12,face = "bold"),
    axis.ticks.y = element_blank(),
    axis.title.y = element_blank(),
    strip.background = element_rect(fill = "transparent"),
    strip.placement = "outside",
    strip.text.y = element_text(angle = 360),
    legend.text=element_text(size=10)
  ) +
  scale_color_manual(name = "", breaks=c("BES", "CHES"), 
                     labels = c("British Election Study", "Chapel Hill Expert Survey"), 
                     values = c("#808080", "#0000FF"))  +
  ylab("Left (0)             ──             Right (10)") +
  theme(legend.position="top") 


# Another version without `(0) and (1)`. This will be replaced with the final 
# version of the manuscript.

p2 <- ggplot(lr_waves, aes(x = factor(wave), y = value, colour = type)) +
  coord_flip() +
  geom_text(aes(label = party), show.legend = TRUE, fontface = 'bold') +
  theme(axis.title.y = element_blank()) +
  facet_grid(vars(wave), scales = "free", labeller = label_both) +
  scale_y_continuous(breaks = pretty_breaks(), limits = c(0, 10)) +
  theme(
    plot.title = element_text(hjust = -1),
    panel.border = element_blank(),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    axis.line = element_line(colour = "black")
  ) +
  theme(
    plot.title = element_text(size = 14, face = "bold"),
    text = element_text(size = 14),
    axis.text.x = element_text(size = 12),
    axis.text.y = element_blank(),
    axis.title = element_text(size = 12, face = "bold"),
    axis.ticks.y = element_blank(),
    strip.background = element_rect(fill = "transparent"),
    strip.placement = "outside",
    strip.text.y = element_text(angle = 360),
    legend.text = element_text(size = 10)
  ) +
  scale_color_manual(
    name = "",
    breaks = c("BES", "CHES"),
    labels = c("British Election Study", "Chapel Hill Expert Survey"),
    values = c("#808080", "#0000FF")
  ) +
  ylab("Left             ──             Right ") +
  theme(legend.position = "top")


ggsave("figure1_waves.pdf", plot = p1, width = 8, height = 5, dpi = 300)
ggsave("figure1_waves_2.pdf", plot = p1, width = 8, height = 5, dpi = 300)

